package com.zwy.rxjava;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 异步联网
 */
public class RxJavaDemo5 extends Activity implements OnClickListener {
	private TextView tvs;
	private TextView tve;
	private TextView tvc;
	private Button btn1;
	private Button btn2;
	private Button btn3;
	public static final String HOST = "http://www.weather.com.cn/data/sk/"; // 这是天气网站
	private List<String> urls;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rxjava2);
		tvs = (TextView) findViewById(R.id.tvs_rx2);
		tve = (TextView) findViewById(R.id.tve_rx2);
		tvc = (TextView) findViewById(R.id.tvc_rx2);
		btn1 = (Button) findViewById(R.id.btn1_rx2);
		btn2 = (Button) findViewById(R.id.btn2_rx2);
		btn3 = (Button) findViewById(R.id.btn3_rx2);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		findViewById(R.id.btn4_rx2).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn1_rx2:
				test1();
				break;
			case R.id.btn2_rx2:
				test2();
				break;
			case R.id.btn3_rx2:
				break;
			case R.id.btn4_rx2:
				break;
			default:
				break;
		}
	}

	private void test1() {
		Observable.just("101010100.html").map(new Func1<String, String>() {
			@Override
			public String call(String s) {
				return HOST + s;
			}
		}).map(new Func1<String, String>() {
			@Override
			public String call(String s) {
				return doNetTask(s);
			}
		}).subscribeOn(Schedulers.newThread())//
				.observeOn(AndroidSchedulers.mainThread())//
				.subscribe(new Action1<String>() {//
					@Override
					public void call(String s) {
						Log.i("zwy", s);
					}
				});
	}

	private void test2() {
		urls = new ArrayList<String>();
		urls.add("101030100.html");
		urls.add("101040100.html");
		urls.add("101050101.html");
		urls.add("101060101.html");
		urls.add("101070101.html");

		Observable.just(urls).flatMap(new Func1<List<String>, Observable<?>>() {
			@Override
			public Observable<?> call(List<String> strings) {
				return Observable.from(strings);
			}
		}).cast(String.class)//
				.map(new Func1<String, String>() {///
					@Override
					public String call(String s) {
						return doNetTask(HOST + s); // 取出想要的字段，这里我就不取出来了
					}
				}).subscribeOn(Schedulers.newThread())//
				.observeOn(AndroidSchedulers.mainThread())//
				.subscribe(new Action1<String>() {//
					@Override
					public void call(String s) {
						Log.i("zwy", "value: " + s);
					}
				});
	}

	private String doNetTask(String s) {

		HttpClient client = new DefaultHttpClient();

		HttpGet get = new HttpGet(s);
		String result;
		try {
			HttpResponse response = client.execute(get);

			if (200 == response.getStatusLine().getStatusCode()) {
				result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
			} else {
				result = "状态行非200";
			}

		} catch (Exception e1) {
			result = e1.getStackTrace().toString();
		}
		return result;

	}
}
